package miage.m1.plugins;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;
// TODO delete this file

/**
 * User: Teffaha Mortadha
 * Date: 12/30/12
 * Time: 9:01 PM
 */
public abstract class AbstractDataSheet extends AbstractTableModel {
    public static Class<?> currentDataSheetClass;
    private static List<AbstractDataSheet> sheets;
    private static int COUNT_SHEETS = 0;
    private static int LAST_ID=0;
    private int ID;
    public AbstractDataSheet(){
            ID = ++AbstractDataSheet.LAST_ID;
    }

    public int getID(){
        return this.ID;
    }

    public static AbstractDataSheet getNewInstance(){
        if(AbstractDataSheet.sheets == null)
            AbstractDataSheet.sheets = new ArrayList<AbstractDataSheet>();
        AbstractDataSheet ads = null;
        try {
            ads = (AbstractDataSheet) AbstractDataSheet.currentDataSheetClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IllegalAccessException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        AbstractDataSheet.COUNT_SHEETS++;
        AbstractDataSheet.sheets.add(ads);
        // we return the last sheet
        return AbstractDataSheet.sheets.get(AbstractDataSheet.sheets.size()-1);
    }
    public static AbstractDataSheet getSheetByIndex(int index){
          if(index > 0 && index < AbstractDataSheet.sheets.size()){
              return AbstractDataSheet.sheets.get(index);
          }
        return null;
    }
    public static void deleteSheetByIndex(int index){
        if(index > 0 && index < AbstractDataSheet.sheets.size()){
            AbstractDataSheet.sheets.remove(index);
            AbstractDataSheet.COUNT_SHEETS --;
            return;
        }
            throw new IndexOutOfBoundsException();
    }
    public static int getSheetsCount(){
        return AbstractDataSheet.COUNT_SHEETS;
    }

    public abstract String getName();
    public abstract void setName(String name);
    public abstract int getRowCount();
    public abstract int getColumnCount();
    public abstract Object getValueAt(int rowIndex, int columnIndex);
    public abstract void setValueAt(Object value, int rowIndex, int columnIndex);
    public boolean isCellEditable(int row, int col)
    { return true; }

}
